home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-03 / ansiterm.zip / ANSITERM.BAS < prev    next >
BASIC Source File  |  1991-03-15  |  11KB  |  440 lines

  1. '=============================================================================
  2. '  AnsiTerm.Bas
  3. '
  4. '  Terminal program with ANSI emulation and XModem file transfer.
  5. '
  6. '  Copyright (c) 1991 Clearware Computing, By David Cleary
  7. '
  8. '  Compile: BC /O AnsiTerm;
  9. '  Link:    LINK /NOD/NOE/EX/PACKC/F AnsiTerm + AnsiDisp + FileSpec + Xmodem +
  10. '           XStat + _NOVAL+_NOREAD+_NOERROR, , NUL, PDQCOMM PDQ;
  11. '=============================================================================
  12.  
  13. DEFINT A-Z
  14.  
  15. '$INCLUDE: 'PDQDecl.Bas'               'PDQ Declarations
  16. '$INCLUDE: 'CommDecl.Bas'              'PDQComm Declarations
  17. '$INCLUDE: 'Term.Bi'                   'Both of these needed for
  18. '$INCLUDE: 'Ansi.Bi'                   'ANSI emulation
  19.  
  20. ' BASIC Declares
  21. DECLARE FUNCTION GetFileSpec$ ()
  22. DECLARE FUNCTION XModemSend% (FileName$)
  23. DECLARE FUNCTION MessageSend% (FileName$, AutoWrap%)
  24. DECLARE FUNCTION XModemReceive% (FileName$)
  25. DECLARE SUB ReadDialDir (FileName$, Numbers$())
  26.  
  27. '-------- Initialize some variables
  28.  
  29.     DIM DialDir$(1 TO 10)
  30.  
  31.     One = 1                             'Saves a few bytes to declare
  32.     Bottom = 25                         'these as variable instead of
  33.     Norm = 7                            'constants
  34.     Reverse = 112
  35.     Space = 32
  36.     Reset$ = "ATZ" + CHR$(13)
  37.     Stat$ = SPACE$(80)
  38.     Menu$ = "ALT +: Quit  Dial  HangUp  Configure  Log:    Upload  dOwnload"
  39.  
  40.     CLS
  41.     AnsiInit                            'This sets the defaults for emulation
  42.     Ansi.BRow = 24                      'We want to use the bottom row for status
  43.  
  44. '-----  Look for configuration file
  45.     Path$ = GetFileSpec$                        'Let's search for a configuration file
  46.     Config$ = LEFT$(Path$, INSTR(Path$, ".")) + "CFG"
  47.  
  48.     IF PDQExist(Config$) THEN           'If we have a configuration file,
  49.         OPEN Config$ FOR BINARY AS #1
  50.         GET #1, , ComNum                 'This is our Comm Port
  51.         GET #1, , Baud&                  'This is our baud rate
  52.         GET #1, , Tone                   'This tells us if we have touch-tone
  53.         CLOSE #1                         'or pulse dialing
  54.     ELSE
  55.         GOSUB GetConfig                  'If no configuration file exists,
  56.     END IF
  57.  
  58. '-----  Read dialing directory if one exists
  59.     DialFile$ = LEFT$(Path$, INSTR(Path$, ".")) + "DIR"
  60.     IF PDQExist(DialFile$) THEN         'See if one exists
  61.         CALL ReadDialDir(DialFile$, DialDir$())  'Read it up
  62.     END IF
  63.  
  64.     GOSUB OpenPort                      'Go and open the port
  65.  
  66.     GOSUB Menu                          'Display Menu
  67.  
  68.     DO
  69.         KeyPress = BIOSInkey             'This gets keyboard input
  70.         SELECT CASE KeyPress             'While this prcesses it
  71.  
  72. '-----  Exit Program   ALT-Q
  73.             CASE -16
  74.                 LSET Stat$ = "Exit to DOS? (Y/N)"
  75.                 Noise = -1
  76.                 GOSUB DoStat               'Prompt with sound
  77.                 LOCATE Bottom, 20
  78.                 GOSUB YesNo                'Yes/No answer
  79.                 IF KeyPress = 78 THEN
  80.                     GOSUB Menu              'If no, display menu
  81.                     AnsiPrint ""            'and reposition cursor
  82.                 ELSE
  83.                     EXIT DO
  84.                 END IF
  85.  
  86. '-----  Dial Out       ALT-D
  87.             CASE -32
  88.                 Phone$ = SPACE$(30)
  89.  
  90.                 LSET Stat$ = "Enter Number to Dial: - "
  91.                 GOSUB DoStat
  92.                 LOCATE Bottom, Bottom
  93.                 BIOSInput Phone$, Reverse
  94.  
  95.                 Phone$ = LTRIM$(RTRIM$(Phone$))
  96.                 GOSUB Menu
  97.                 AnsiPrint ""
  98.                 IF LEN(Phone$) THEN GOSUB DialOut
  99.  
  100. '-----  Hang Up        ALT-H
  101.             CASE -35
  102.                 LSET Stat$ = "Hanging Up Modem - StandBy"
  103.                 GOSUB DoStat
  104.  
  105.                 Pause Bottom
  106.                 ComPrint "+++"
  107.                 Pause Bottom
  108.                 ComPrint "ATH" + CHR$(13)
  109.                 GOSUB Menu
  110.  
  111. '-----  Change Configuration   ALT-C
  112.             CASE -46
  113.                 CloseCom
  114.                 GOSUB GetConfig
  115.                 GOSUB OpenPort
  116.                 GOSUB Menu
  117.  
  118. '-----  Log To Disk    ALT-L
  119.             CASE -38
  120.  
  121.                 IF LogOn THEN              'If we are logging, then we stop
  122.                     LogOn = 0
  123.                     CLOSE #1                'Close our log file
  124.                     DosErr = ERR            'Check for errors
  125.                     IF DosErr THEN
  126.                         LSET Stat$ = "Disk Error - " + PDQMessage$(DosErr)
  127.                         Noise = -1
  128.                         GOSUB DoStat
  129.                     ELSE
  130.                         LSET Stat$ = "Closing " + FileName$
  131.                         GOSUB DoStat
  132.                     END IF
  133.                     Pause Bottom
  134.  
  135.                 ELSE
  136.                                                     'Else start logging
  137.                     FileName$ = SPACE$(30)
  138.                     LSET Stat$ = "Enter File Name:"
  139.                     GOSUB DoStat
  140.                     LOCATE Bottom, 18
  141.                     BIOSInput FileName$, Reverse
  142.  
  143.                     FileName$ = UCASE$(RTRIM$(LTRIM$(FileName$)))
  144.                     LenName = LEN(FileName$)
  145.  
  146.                     IF LenName THEN
  147.                         OverWrite = -1
  148.                         IF PDQExist(FileName$) THEN
  149.                             LSET Stat$ = FileName$ + " Exists, Overwrite it? (Y/N)"
  150.                             Noise = -1
  151.                             GOSUB DoStat
  152.                             LOCATE Bottom, LenName + 30
  153.                             GOSUB YesNo
  154.                             IF KeyPress = 78 THEN OverWrite = 0
  155.  
  156.                             IF NOT OverWrite THEN
  157.                                 LSET Stat$ = "Append to " + FileName$ + "? (Y/N)"
  158.                                 GOSUB DoStat
  159.                                 LOCATE Bottom, LenName + 19
  160.                                 GOSUB YesNo
  161.                                 AppendTo = -1
  162.                                 IF KeyPress = 78 THEN AppendTo = 0
  163.                             END IF
  164.                         END IF
  165.  
  166.                         IF OverWrite THEN
  167.                             OPEN FileName$ FOR OUTPUT AS #1
  168.                             DosErr = ERR
  169.                         ELSEIF AppendTo THEN
  170.                             OPEN FileName$ FOR APPEND AS #1
  171.                             DosErr = ERR
  172.                         END IF
  173.  
  174.                         IF DosErr THEN
  175.  
  176.                             LSET Stat$ = "Disk Error - " + PDQMessage$(DosErr)
  177.                             GOSUB DoStat
  178.                             LogOn = 0
  179.                             Pause Bottom
  180.  
  181.                         ELSEIF OverWrite OR AppendTo THEN
  182.  
  183.                             LogOn = -1
  184.                             LSET Stat$ = "Opening: " + FileName$
  185.                             GOSUB DoStat
  186.                             Pause Bottom
  187.  
  188.                         END IF
  189.                     END IF
  190.                 END IF
  191.  
  192.                 GOSUB Menu
  193.                 AnsiPrint ""
  194.  
  195. '-----  Function Keys for Dialing Directory  F1 - F10
  196.             CASE -66 TO -59
  197.                 Phone$ = DialDir$(-KeyPress - 58)
  198.                 IF LEN(Phone$) THEN GOSUB DialOut
  199.  
  200. '-----  Upload a File
  201.             CASE -22
  202.  
  203.                 FileName$ = SPACE$(30)
  204.                 LSET Stat$ = "Enter File Name to Upload:"
  205.                 GOSUB DoStat
  206.                 LOCATE Bottom, 28
  207.                 BIOSInput FileName$, Reverse
  208.                 FileName$ = UCASE$(LTRIM$(RTRIM$(FileName$)))
  209.                 IF NOT PDQExist(FileName$) THEN
  210.  
  211.                     AnsiPrint ""
  212.                     LSET Stat$ = FileName$ + "Not Found . . ."
  213.                     Noise = -1
  214.                     GOSUB DoStat
  215.                     Pause Bottom
  216.  
  217.                 ELSE
  218.  
  219.                     AnsiPrint ""
  220.                     GOSUB Menu
  221.                     SetHandshaking "NON"
  222.                     RetCode = XModemSend(FileName$)
  223.                     SetHandshaking "XON"
  224.                     IF RetCode THEN
  225.                         LSET Stat$ = "Upload failed - Return Code " + STR$(RetCode)
  226.                     ELSE
  227.                         LSET Stat$ = "Upload successful"
  228.                     END IF
  229.                     Noise = -1
  230.                     GOSUB DoStat
  231.                     Pause Bottom
  232.  
  233.                 END IF
  234.                 GOSUB Menu
  235.  
  236. '-----  Download a File
  237.             CASE -24
  238.  
  239.                 FileName$ = SPACE$(30)
  240.                 LSET Stat$ = "Enter File Name to Download:"
  241.                 GOSUB DoStat
  242.                 LOCATE Bottom, 31
  243.                 BIOSInput FileName$, Reverse
  244.                 FileName$ = UCASE$(LTRIM$(RTRIM$(FileName$)))
  245.                 AnsiPrint ""
  246.                 GOSUB Menu
  247.                 SetHandshaking "NON"
  248.                 RetCode = XModemReceive(FileName$)
  249.                 SetHandshaking "XON"
  250.                 IF RetCode THEN
  251.                     LSET Stat$ = "Download failed - Return Code " + STR$(RetCode)
  252.                 ELSE
  253.                     LSET Stat$ = "Download successful"
  254.                 END IF
  255.                 Noise = -1
  256.                 GOSUB DoStat
  257.                 Pause Bottom
  258.  
  259.                 GOSUB Menu
  260.  
  261. '-----  Normal keys out the port
  262.             CASE IS > 0
  263.                 ComPrint CHR$(KeyPress)
  264.  
  265. '-----  Extended key code
  266.             CASE IS < 0
  267.                 ComPrint CHR$(0) + CHR$(KeyPress)
  268.  
  269.         END SELECT
  270.  
  271. '-----  Servive Port
  272.         IF ComLoc THEN
  273.             Text$ = ComInput$(ComLoc)
  274.             AnsiPrint Text$
  275.             IF LogOn THEN PUT #1, , Text$
  276.         END IF
  277.  
  278. '-----  IMPORTANT !!! Check for buffer overrun condition
  279.         IF OverRun THEN
  280.             LSET Stat$ = "Buffer Overrun !!!"
  281.             Noise = -1
  282.             GOSUB DoStat
  283.             FlushBuffer
  284.             Pause Bottom
  285.             GOSUB Menu
  286.         END IF
  287.  
  288.     LOOP
  289.  
  290.     CloseCom
  291.     IF LogOn THEN CLOSE #1
  292.     CLS
  293.     END
  294.  
  295. '*****************************************************************************
  296. '-----  Subroutines
  297.  
  298. Menu:                                  'display line 25 menu
  299.     LSET Stat$ = ""
  300.     PDQPrint Stat$, Bottom, One, Reverse   'clear line 25
  301.     PDQPrint Menu$, Bottom, One, Reverse   'print the menu string
  302.  
  303.     PDQPrint "Q", Bottom, 8, Norm       'highlight menu keys
  304.     PDQPrint "D", Bottom, 14, Norm
  305.     PDQPrint "H", Bottom, 20, Norm
  306.     PDQPrint "C", Bottom, 28, Norm
  307.     PDQPrint "L", Bottom, 39, Norm
  308.     PDQPrint "U", Bottom, 47, Norm
  309.     PDQPrint "O", Bottom, 56, Norm
  310.  
  311.     IF LogOn THEN                          'if disk log is on then
  312.         PDQPrint "On", Bottom, 43, Reverse  'highlight on
  313.     ELSE                                   'if disk log is off then
  314.         PDQPrint "Off", Bottom, 43, Reverse 'highlight off
  315.     END IF
  316. RETURN
  317.  
  318. YesNo:
  319.     YesNoCol = POS(0)                   'save cursor column
  320.     DO                                  'key press loop
  321.         LOCATE Bottom, YesNoCol          'locate curosr position
  322.         KeyPress = BIOSInkey             'get user input
  323.         IF KeyPress THEN                 'if a key was pressed
  324.             IF KeyPress > 90 THEN KeyPress = KeyPress - 32  'upper case swap
  325.             PRINT CHR$(KeyPress);         'print the key
  326.         END IF
  327.     LOOP UNTIL KeyPress = 89 OR KeyPress = 78   'loop until a valid choice
  328. RETURN
  329.  
  330. '----- Reconfigure
  331. GetConfig:
  332.     LSET Stat$ = "Enter Com Port Number - "
  333.     GOSUB DoStat
  334.     LOCATE Bottom, Bottom
  335.     DO
  336.         KeyPress = BIOSInkey
  337.         IF KeyPress THEN
  338.             IF KeyPress = 27 THEN
  339.                 LOCATE Row, Col
  340.                 GOSUB Menu
  341.                 RETURN
  342.             END IF
  343.             PDQPrint CHR$(KeyPress), Bottom, Bottom, Norm
  344.             ComNum = PDQValI(CHR$(KeyPress))
  345.             IF ComNum < 1 AND ComNum > 4 THEN
  346.                 PDQSound 2500, 2
  347.             ELSE
  348.                 EXIT DO
  349.             END IF
  350.         END IF
  351.     LOOP
  352.  
  353.     LSET Stat$ = "What baud rate do you want? "
  354.     GOSUB DoStat
  355.     Baud$ = SPACE$(5)
  356.     LOCATE Bottom, 29
  357.     BIOSInput Baud$, Reverse
  358.     Baud& = PDQValL(Baud$)
  359.  
  360.     LSET Stat$ = "Do you have a touch-tone phone? (Y/N) - "
  361.     GOSUB DoStat
  362.     LOCATE Bottom, 40
  363.     GOSUB YesNo
  364.     IF KeyPress = 89 THEN Tone = -1 ELSE Tone = 0
  365.  
  366.     OPEN Config$ FOR BINARY AS #2
  367.     PUT #2, , ComNum
  368.     PUT #2, , Baud&
  369.     PUT #2, , Tone
  370.     CLOSE #2
  371.  
  372.     GOSUB Menu
  373.     AnsiPrint ""
  374. RETURN
  375.  
  376. '-----  Open Com Port
  377. OpenPort:
  378.     LSET Stat$ = "Initializing Port - Standby"
  379.     GOSUB DoStat
  380.     OpenCom "COM" + STR$(ComNum) + ":" + STR$(Baud&) + ",N,8,1,RB1024,XON"
  381.     ComErr = ERR
  382.     IF ComErr THEN
  383.         LSET Stat$ = "Sorry - Communications error " + STR$(ComErr)
  384.         Noise = -1
  385.         GOSUB DoStat
  386.         Pause Bottom
  387.         CLS
  388.         END
  389.     END IF
  390.  
  391.     ComPrint Reset$
  392.  
  393. RETURN
  394.  
  395. '----- Dial Out
  396. DialOut:
  397.     IF Tone THEN Dial$ = "ATDT" ELSE Dial$ = "ATDP"
  398.     Dial$ = Dial$ + Phone$ + CHR$(13)
  399.     LSET Stat$ = "Dialing " + Phone$
  400.     GOSUB DoStat
  401.     ComPrint Dial$
  402.     Pause Bottom
  403.     GOSUB Menu
  404. RETURN
  405.  
  406. '-----  Print staus line
  407. DoStat:
  408.     PDQPrint Stat$, Bottom, One, Norm
  409.     IF Noise THEN
  410.         PDQSound 2500, 2
  411.         Noise = 0
  412.     END IF
  413. RETURN
  414.  
  415. SUB ReadDialDir (FileName$, Numbers$()) STATIC
  416.  
  417.     MaxNum = UBOUND(Numbers$)           'Determine max amount of numbers
  418.     CurNum = 1                          'Array pointer
  419.     FileNum = FREEFILE                  'Get a file handle
  420.     OPEN FileName$ FOR INPUT AS #FileNum 'Open the file
  421.  
  422.     DO
  423.         LINE INPUT #FileNum, Line$       'Read a line
  424.         Comment = INSTR(Line$, "'")      'Parse out comments
  425.         IF Comment THEN
  426.             Line$ = LEFT$(Line$, Comment - 1)
  427.         END IF
  428.         Line$ = LTRIM$(RTRIM$(Line$))    'Remove all spaces
  429.  
  430.         IF LEN(Line$) THEN               'After all this, if we have anything
  431.             Numbers$(CurNum) = Line$      'Lets call it a phone number
  432.             CurNum = CurNum + 1
  433.         END IF
  434.     
  435.     LOOP UNTIL EOF(FileNum) OR CurNum > MaxNum 'Continue until we reach the
  436.                                                              'the enod of file or out of
  437.     CLOSE #FileNum                             'numbers
  438.  
  439. END SUB
  440.